<?php
require_once('Extension/Controller/Plugin/Abstract.php');

class Extension_Controller_Plugin_Common_Layout extends Extension_Controller_Plugin_Abstract
{
    //public function routeStartup(Zend_Controller_Request_Abstract $request) {}

    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        self::initLayout($request);
    }

    //public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {}
    //public function preDispatch(Zend_Controller_Request_Abstract $request) {}
    //public function postDispatch(Zend_Controller_Request_Abstract $request) {}
    //public function dispatchLoopShutdown() {}


    public static function initLayout(Zend_Controller_Request_Abstract $request)
    {
        $moduleName = $request->getModuleName();
        
        if (!Zend_Registry::isregistered('layout')) {

            $config = Zend::initConfig('Layout');

            if (null != $config->$moduleName) {
                $layout = Zend_Layout::startMvc()->setLayoutPath(PROJECT_ROOT . $config->$moduleName->layoutPath)
                                                 ->setLayout($config->$moduleName->layout);

                Zend_Registry::set('layout', $layout);
                return $layout;
            } else {
                return false;
            }

        } else {
            return Zend_Registry::get('layout');
        }
    }
}